var callSunburstConstructor = function(i) {
	document.getElementById("size").className = "";
	document.getElementById("count").className = "active";
	document.getElementById("chart").innerHTML= "";
	var sunburstJsonType;
	switch(i) {
		case 0:
			sunburstJsonType = "./json/sunburstAP.json";
			document.getElementById("AP").className = "active";
			document.getElementById("ARP").className = "";
			document.getElementById("ATP").className = "";
			break;
		case 1:
			sunburstJsonType = "./json/sunburstARP.json";
			document.getElementById("ARP").className = "active";
			document.getElementById("AP").className = "";
			document.getElementById("ATP").className = "";
			break;
		case 2:
			sunburstJsonType = "./json/sunburstATP.json";
			document.getElementById("ATP").className = "active";
			document.getElementById("ARP").className = "";
			document.getElementById("AP").className = "";
			break;
	}
	getSunburst(sunburstJsonType);
}
var arc;
function getSunburst(sunburstJson) {
   document.getElementById("durationButton").style.display = "block";
   startNav();
   changeActive("sunburst");
   document.getElementById("chart").innerHTML = "";
   document.getElementById("description").innerHTML = "";
   document.getElementById("description").style.display = "none";

   var w = 960,
      h = 700,
      r = Math.min(w, h) / 2;
   var color = d3.scale.category33();

   var vis = d3.select("#chart").append("svg").attr("width", w).attr("height", h).append("g").attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");

   var partition = d3.layout.partition().sort(null).size([2 * Math.PI, r * r]).value(function (d) {
      return 1;
   });
   arc = d3.svg.arc().startAngle(function (d) {
      return d.x;
   }).endAngle(function (d) {
      return d.x + d.dx;
   }).innerRadius(function (d) {
      return Math.sqrt(d.y);
   }).outerRadius(function (d) {
      return Math.sqrt(d.y + d.dy);
   });

   d3.json(sunburstJson, function (json) {
      var path = vis.data([json]).selectAll("path").data(partition.nodes).enter().append("path").attr("display", function (d) {
         return d.depth ? null : "none";
      })
      /////////////////ADD ON MOUSEOVER HANDLER TO THE ARCS///////////////////////
      .on("mouseover", function (d) {
         var label;
         if (d.name != undefined) {
            label = "<p>" + d.name + "</p>";
         } else {
            label = "<p>" + d.label + "</p>";
         }
         // setButtons("<br><br>You are looking at :<br>" + label)
         d3.select(this).style("stroke", "black");
         document.getElementById("arcsLabel").style.display = "block";
         document.getElementById("arcsLabel").style.top = event.clientX;
         document.getElementById("arcsLabel").style.left = event.clientX;
         document.getElementById("arcsLabel").innerHTML = label;
         // document.getElementById("durationButton").style.display = "block";
      })
      /////////////////ADD ON CLICK HANDLER TO THE ARCS///////////////////////
      .on("click", function (d) {
         var project;
         if (d.label != undefined) {
            project = 'Project: ' + d.label + '<br />';
            project += "Duration: " + d.size + " months <br /><br />";
            project += getDescription(d.responsible) + '<br>';
         } else {
            project = d.name;
         }
         if (d.children != undefined) {
            project += "<br /><ul>";
            for (var i = 0; i < d.children.length; i++) {
               if (d.children[i].name != undefined) {
                  project += "<li>" + d.children[i].name + "</li>";
               } else {
                  project += '<li>' + d.children[i].label + '  <button class="seeMoreButtons" onclick="showDescription(\'' +
                   d.children[i].responsible + "','" + d.children[i].label + "','" + d.children[i].size +
                   '\')">see more...</button></li>';
               }
            };
            project += "</ul>";
         }
         document.getElementById("description").innerHTML = project;
         document.getElementById("description").style.display = "block";
       })

      /////////////////////////ADD TEXT INSIDE ARCS/////////////////////////////////////////////
      // .append("svg:text").text(function(d, i) {
      // if(d.value != undefined) {
      // return d.value;
      // };
      // })
      // .attr("transform", function(d) {
      // return "translate(" + arc.centroid(d) + ")";
      // }).attr("dy", ".35em").attr("text-anchor", "middle")
      /////////////////////ADD ON MOUSE OUT////////////////////////////
      .on("mouseout", function () {
         d3.select(this).style("stroke", "white");
         document.getElementById("arcsLabel").style.display = "none";
      })
      /////////////////////////////////////////////////////////////////////////////
      // hide inner ring
      .attr("d", arc).attr("fill-rule", "evenodd").style("stroke", "#fff").style("fill", function (d) {
        return getSunburstColors(d);
      }).each(stash)
      ////////////ADD ID TO THE ARCS//////////////////////////
      .attr("class", function(d) {
      		if(d.label != undefined) {
				return "project";
			} 
			if(d.typeOf == "type") {
				return "type";
			}
			if(d.typeOf == "responsible") {
				return "responsible";
			}
			if(d.type == "area") {
				return "area";
			}	
      	}
	);

      d3.select("#size").on("click", function () {
         path.data(partition.value(function (d) {
            return d.size;
         })).transition().duration(1500).attrTween("d", arcTween);

         d3.select("#size").classed("active", true);
         d3.select("#count").classed("active", false);
      });

      d3.select("#count").on("click", function () {
         path.data(partition.value(function (d) {
            return 1;
         })).transition().duration(1500).attrTween("d", arcTween);

         d3.select("#size").classed("active", false);
         d3.select("#count").classed("active", true);
      });

   });
}


// Stash the old values for transition.
function stash(d) {
	d.x0 = d.x;
	d.dx0 = d.dx;
}

// Interpolate the arcs in data space.
function arcTween(a) {
	var i = d3.interpolate({
		x : a.x0,
		dx : a.dx0
	}, a);
	return function(t) {
		var b = i(t);
		a.x0 = b.x;
		a.dx0 = b.dx;
		return arc(b);
	};
}

var green = 0;
var lightBlue = 0;
var red = 0;
var yellow = 0;
var brown = 0;
var gold = 0;
var silver = 0;
var purple = 0;

var getSunburstColors = function(d) {
		var returnColor;
		if(d.parent != undefined) {
			//////COMPUTATIONAL SCIENCE
			if(d.name == "Computational Science") {
				green = 60;
				returnColor = "green";
			}
			if(d.parent.name == "Computational Science") {
				returnColor = "rgb(30, "+green+", 0)";
				green += 30;
			}
			if(d.parent.parent != undefined && d.parent.parent.name == "Computational Science") {
				returnColor = "rgb(0,"+ green +",0)";
				green += 30;
			}
			///////THEORETICAL COMPUTER SCIENCE
			if(d.name == "Theoretical Computer Science") {
				blue = 60;
				returnColor = "blue"; 
			}
			if(d.parent.name == "Theoretical Computer Science") {
				returnColor = "rgb(0,0,"+ blue +")";
				blue += 30;
			}
			if(d.parent.parent != undefined && d.parent.parent.name == "Theoretical Computer Science") {
				returnColor = "rgb(0,0,"+ blue +")";
				blue += 30;
			}
			//////INTELLIGENT SYSTEMS
			if(d.name == "Intelligent Systems") {
				red = 60;
				returnColor = "red"; 
			}
			if(d.parent.name == "Intelligent Systems") {
				returnColor = "rgb("+ red +",0,0)";
				red += 30;
			}
			if(d.parent.parent != undefined && d.parent.parent.name == "Intelligent Systems") {
				returnColor = "rgb("+ red +",0,0)";
				red += 30;
			}
			///////OTHER
			if(d.name == "Other") {
				yellow = 60;
				returnColor = "yellow"; 
			}
			if(d.parent.name == "Other") {
				returnColor = "rgb("+ yellow +","+ yellow +",0)";
				yellow += 30;
			}
			if(d.parent.parent != undefined && d.parent.parent.name == "Other") {
				returnColor = "rgb("+ yellow +","+ yellow +",0)";
				yellow += 30;
			}
			////////SOFTWARE ENGINEERING
			if(d.name == "Software Engineering") {
				brown = 0;
				returnColor = "rgb(120,0,30)"; 
			}
			if(d.parent.name == "Software Engineering") {
				returnColor = "rgb(120,"+ brown +","+(brown+10) +")";
				brown += 2;
			}
			if(d.parent.parent != undefined && d.parent.parent.name == "Software Engineering") {
				returnColor = "rgb(120,"+brown +","+(brown+10) +")";
				brown += 2;
			}
			//////INFORMATION SYSTEMS
			if(d.name == "Information Systems") {
				lightBlue = 0;
				returnColor = "rgb(0,220,220)"; 
			}
			if(d.parent.name == "Information Systems") {
				returnColor = "rgb("+ lightBlue +","+ (lightBlue+200) +"," + (lightBlue+200) + ")";
				lightBlue += 5;
			}
			if(d.parent.parent != undefined && d.parent.parent.name == "Information Systems") {
				returnColor = "rgb("+ lightBlue +","+ (lightBlue+200) +"," + (lightBlue+200) + ")";
				lightBlue += 5;
			}
			//////DISTRIBUTED SYSTEMS
			if(d.name == "Distributed Systems") {
				silver = 80;
				returnColor = "silver";
			}
			if(d.parent.name == "Distributed Systems") {
				returnColor = "rgb("+ silver + ","+ silver+"," + silver+")";
				silver += 5;
			}
			if(d.parent.parent != undefined && d.parent.parent.name == "Distributed Systems") {
				returnColor = "rgb("+ silver + ","+ silver+"," + silver+")";
				silver += 5;
			}
			/////EMBEDDED SYSTEMS
			if(d.name == "Embedded Systems") {
				purple = 60;
				returnColor = "purple"; 
			}
			if(d.parent.name == "Embedded Systems") {
				returnColor = "rgb(200,"+ purple+",250)";
				purple += 10;
			}
			if(d.parent.parent != undefined && d.parent.parent.name == "Embedded Systems") {
				returnColor = "rgb(200,"+ purple+",250)";
				purple += 5;
			}
			return returnColor;
		}
}
